#### Table A12 Script ####
rm(list=ls());gc();gc();gc();gc();gc();gc();gc();gc()


library(gt)
library(Hmisc)
library(tidyverse)
library(dplyr)
library(hrbrthemes)
library(lfe)
library(stargazer)

here::i_am("Scripts/TabA12Script.R")

library(here)

#Get some descriptives about these two groups in our sample


df<-readRDS(here("Data", "acrossanalysisset.rds"))


#Limit to just those non-event donors who gave more than once

#Get a designation for event and non-event donors
df<-df %>%
  group_by(finstate3dc)%>%
  dplyr::mutate(edon=ifelse(sum(fundraiser)>0,1,0))

#Find out how many donations each person has

df<-df %>%
  group_by(finstate3dc)%>%
  dplyr::mutate(numdons=n())

tabnumdonsedon<-table(df$edon, df$numdons)

#Also get a total for how much total they've donated

df<-df %>%
  group_by(finstate3dc)%>%
  dplyr::mutate(totdonamt=sum(cAMOUNT))


#Get summary statistics on these donors

# Break down number of donations and total amount given, as well as min and max of each, by type of giver

dfuniq<-df%>%
  select(finstate3dc, totdonamt,numdons, edon)%>%
  unique()

dfuniq$edon<-ifelse(dfuniq$edon==1, "Event Donor", "Non-Event Donor")

summtable<-dfuniq%>%
  group_by(edon)%>%
  summarise(meannum=round(mean(numdons),2), mediandons=median(numdons), maxnum=max(numdons), q25amt=unname(quantile(totdonamt)[2]), medianamt=median(totdonamt),q75amt=unname(quantile(totdonamt)[4]), meanamt=mean(totdonamt), maxamt=max(totdonamt))


#Got to do wihtin analysis as well

df<-readRDS(here("Data", "withinanalysisset.rds"))

#All are event donors, so...
df$edon<-1

#Find out how many donations each person has

df<-df %>%
  group_by(finstate3dc)%>%
  dplyr::mutate(numdons=n())

tabnumdonsedon<-table(df$edon, df$numdons)

#Also get a total for how much total they've donated

df<-df %>%
  group_by(finstate3dc)%>%
  dplyr::mutate(totdonamt=sum(cAMOUNT))

dfuniq<-df%>%
  select(finstate3dc, totdonamt,numdons, edon)%>%
  unique()

dfuniq$edon<-ifelse(dfuniq$edon==1, "Event Donor", "Non-Event Donor")

summtable2<-dfuniq%>%
  group_by(edon)%>%
  summarise(meannum=round(mean(numdons),2), mediandons=median(numdons),maxnum=max(numdons), q25amt=unname(quantile(totdonamt)[2]), medianamt=median(totdonamt),q75amt=unname(quantile(totdonamt)[4]), meanamt=mean(totdonamt), maxamt=max(totdonamt))

summtable<-rbind(summtable,summtable2)

library(gt)

#Create table and save to file:
tab1<-summtable |>
  gt() |>
  tab_header(
    title = "Across Donor Analysis")|>
  tab_spanner(
    label = "Number of Donations",
    columns = c(meannum,mediandons, maxnum)
  ) |>
  tab_spanner(
    label = "Total Amount Given",
    columns = c(q25amt, medianamt, q75amt, meanamt, maxamt)
  ) |>
  fmt_currency(columns=q25amt:maxamt, decimals=0)  |>
  cols_label(
    edon = "Donor Status",
    meannum = "Average ",
    mediandons = "Median",
    maxnum = "Maximum ",
    q25amt = "25th% ",
    medianamt = "Median",
    q75amt = "75th%",
    meanamt = "Mean",
    maxamt="Maximum")|>
  tab_row_group(
    label = "Within-Donor Analysis",
    rows = 3
  )|>
  tab_row_group(
    label = "Across-Donor Analysis",
    rows = 1:2
  )

tab1 |> gtsave(here("Results","TabA12.tex"))

#Just to see the table:
tab1<-summtable |>
  gt() |>
  tab_header(
    title = "Across Donor Analysis")|>
  tab_spanner(
    label = "Number of Donations",
    columns = c(meannum,mediandons, maxnum)
  ) |>
  tab_spanner(
    label = "Total Amount Given",
    columns = c(q25amt, medianamt, q75amt, meanamt, maxamt)
  ) |>
  fmt_currency(columns=q25amt:maxamt, decimals=0)  |>
  cols_label(
    edon = "Donor Status",
    meannum = "Average ",
    mediandons = "Median",
    maxnum = "Maximum ",
    q25amt = "25th% ",
    medianamt = "Median",
    q75amt = "75th%",
    meanamt = "Mean",
    maxamt="Maximum")|>
  tab_row_group(
    label = "Within-Donor Analysis",
    rows = 3
  )|>
  tab_row_group(
    label = "Across-Donor Analysis",
    rows = 1:2
  )|>
  as_latex()

tab1%>%
  as.character() %>%
  cat()

